package com.zjzk.demo.controller;

import com.alibaba.nacos.shaded.com.google.gson.JsonObject;
import com.github.pagehelper.PageInfo;
import com.zjzk.common.core.constant.UserConstants;
import com.zjzk.common.core.web.controller.BaseController;
import com.zjzk.common.core.web.domain.AjaxResult;
import com.zjzk.common.core.web.page.TableDataInfo;
import com.zjzk.common.log.annotation.Log;
import com.zjzk.common.log.enums.BusinessType;
import com.zjzk.common.security.annotation.RequiresPermissions;
import com.zjzk.common.security.utils.SecurityUtils;
import com.zjzk.demo.domain.WorkingRecord;
import com.zjzk.demo.service.IWorkingRecordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/workingRecord")
public class WorkingRecordController extends BaseController {
    @Autowired
    private IWorkingRecordService workingRecordService;

    @GetMapping("/tablelist")
    public TableDataInfo tablelist(WorkingRecord workingRecord){
        startPage();
        List<WorkingRecord> list = workingRecordService.selectWorkingRecordList(workingRecord);
        return getDataTable(list);
    }

    @GetMapping("/list")
    public Map<String, Object> list(WorkingRecord workingRecord){
        startPage();
        List<WorkingRecord> list = workingRecordService.selectWorkingRecordList(workingRecord);
        Map object = new HashMap<String, Object>();
        object.put("code",200);
        object.put("data",list);
        object.put("total",new PageInfo(list).getTotal());
        object.put("count",3);
        return object;
    }

    @GetMapping(value = "/{workingRecordId}")
    public AjaxResult getInfo(@PathVariable Long workingRecordId)
    {
        return AjaxResult.success(workingRecordService.selectWorkingRecordById(workingRecordId));
    }

    @RequiresPermissions("demo:workingRecord:add")
    @Log(title = "井下作业管理", businessType = BusinessType.UPDATE)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody WorkingRecord workingRecord){
        workingRecord.setCreateBy(SecurityUtils.getUsername());
        return toAjax(workingRecordService.insertWorkingRecord(workingRecord));
    }

    @RequiresPermissions("demo:workingRecord:edit")
    @Log(title = "井下作业管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody WorkingRecord workingRecord)
    {
        workingRecord.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(workingRecordService.updateWorkingRecord(workingRecord));
    }

    @RequiresPermissions("demo:workingRecord:remove")
    @Log(title = "井下作业管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{wrIds}")
    public AjaxResult remove(@PathVariable Long[] wrIds)
    {
        return toAjax(workingRecordService.deleteWorkingRecordByIds(wrIds));
    }
}
